﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Common Core Extensions
//===============================================================================

using System;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

namespace EntLibContrib.Common.Configuration.Design
{
	/// <summary>
	/// Represents a designtime node for a particular configuration element sortable by name. This class is abstract.
	/// </summary>
	public abstract class NameSortConfigurationNode : ConfigurationNode, IComparable
	{
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="NameSortConfigurationNode"/> class.
		/// </summary>
		protected NameSortConfigurationNode()
			: base()
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="NameSortConfigurationNode"/> class with a name.
		/// </summary>		
		/// <param name="name">The name of the node.</param>
		protected NameSortConfigurationNode(string name)
			: base(name)
		{
		}
		#endregion

		#region IComparable Methods
		/// <summary>
		/// Compares the specified node to the current node based on the value of the <seealso cref="ConfigurationNode.Name"/> property.
		/// </summary>
		/// <param name="obj">An object to compare.></param>
		/// <returns>
		/// A signed integer that indicates the relative order of this node and the node being compared.
		/// </returns>
		int IComparable.CompareTo(object obj)
		{
			NameSortConfigurationNode compareNode = obj as NameSortConfigurationNode;
			if (compareNode == null)
			{
				return -1;
			}
			return Name.CompareTo(compareNode.Name);
		}

		/// <summary>
		/// Compares the specified node to the current node based on the value of the <seealso cref="ConfigurationNode.Name"/> property.
		/// </summary>
		/// <param name="node">A <see cref="NameSortConfigurationNode"/> to compare.</param>
		/// <returns>
		/// A signed integer that indicates the relative order of this node and the node being compared.
		/// </returns>
		public int CompareTo(NameSortConfigurationNode node)
		{
			return ((IComparable)this).CompareTo(node);
		}
		#endregion
	}
}
